//
//  AddViewController.m
//  book
//
//  Created by cocoazxj@163.com on 15/4/7.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "AddViewController.h"
#import "ContactModel.h"

@interface AddViewController ()
@property (nonatomic, strong) IBOutlet UITextField *tf_name;
@property (nonatomic, strong) IBOutlet UITextField *tf_phoneNum;
@property (nonatomic, strong) IBOutlet UIButton *btn_add;
@property (nonatomic, strong) ContactModel *contactModel;
@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.tf_name];
    [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.tf_phoneNum];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textChange
{
    self.btn_add.hidden = !(self.tf_name.text.length > 0 && self.tf_phoneNum.text.length > 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addButtonClicked:(id)sender
{
    ContactModel *m = [[ContactModel alloc] init];
    m.name = self.tf_name.text;
    m.phoneNum = self.tf_phoneNum.text;
    if([self.delegate respondsToSelector:@selector(addViewControllerDelegate:contactModel:)])
    {
        [self.delegate addViewControllerDelegate:self contactModel:m];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
